package com.sxfq.loansupermarket.backsystem.modular.basicData.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.support.DateTimeKit;
import com.sxfq.loansupermarket.backsystem.core.util.AliyunOssUtil;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.dao.PointRewordDao;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.PointRewordIconinfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.PointRewordInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.SearchPointRewordInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.PointRewordService;
import com.sxfq.loansupermarket.backsystem.core.util.CommUtils;
import com.sxfq.loansupermarket.backsystem.modular.weixin.config.MemberEnumConfig;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.*;

@Service
@Transactional
public class PointRewordServiceImpl implements PointRewordService {
    @Autowired
    private PointRewordDao dao;

    @Autowired
    AliyunOssUtil aliyunOssUtil;

    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<PointRewordInfo> selectPageListByQueryGetMap(Page<PointRewordInfo> page, SearchPointRewordInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getProductName())) {
            params.put("productName", searchInfo.getProductName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getArea())) {
            params.put("area", searchInfo.getArea());
        }
        params.put("startIndex", searchInfo.getStartIndex());
        params.put("limitIndex", searchInfo.getLimitIndex());
        return dao.getQueryPage(page, params);
    }

    @Override
    public int getQueryPagesPointCount(Page<PointRewordInfo> page, SearchPointRewordInfo info) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(info.getProductName())) {
            params.put("productName", info.getProductName());
        }
        if (StringUtil.isNotEmpty(info.getArea())) {
            params.put("area", info.getArea());
        }
        return dao.getQueryPagePointCount(page, params);
    }

    public PointRewordInfo selectOneById(String id) {
        String sql = "select a.*,(select b.area from dept b where b.area_abbreviation=a.area limit 1) as areas from point_reword_info a where id= '" + id + "'";
        PointRewordInfo info = sqlMapper.selectOne(sql, PointRewordInfo.class);
        return info;
    }

    public int insert(PointRewordInfo info) {
        String sql = "INSERT INTO `point_reword_info` (`id`,`product_name`,product_description,product_price,market_price,putaway_number,surplus_number,type,remarks,create_time,number,area) VALUES" +
                "('" + info.getNum() + "','" + info.getProductName().trim() + "' ,'" + info.getProductDescription() + "','" + info.getProductPrice() + "','" + info.getMarketPrice() + "','" + info.getPutawayNumber() + "','" + info.getPutawayNumber() + "'," +
                "'" + info.getType() + "','" + info.getRemarks() + "','" + DateTimeKit.now() + "','" + info.getNumber() + "','" + info.getArea() + "');";
        String updateStr = "update point_reword_iconinfo a set a.product_id='" + info.getNum() + "' where num='" + info.getNum() + "'";
        sqlMapper.update(updateStr, PointRewordIconinfo.class);
        return sqlMapper.insert(sql);
    }

    @Transactional
    public void delete(String id) {
        String sql = "delete from point_reword_info where id = '" + id + "'";
        String dsql = "delete from point_reword_iconinfo where product_id='" + id + "'";
        sqlMapper.delete(sql);
        sqlMapper.delete(dsql);
    }

    public int update(PointRewordInfo info) {
        String updateSql = "UPDATE point_reword_info SET particulars='" + info.getParticulars() + "'" +
                " WHERE id='" + info.getId() + "'";
        return sqlMapper.update(updateSql);
    }

    public int updatePoint(PointRewordInfo info) {
        String updateSql = "UPDATE point_reword_info SET product_name='" + info.getProductName() + "', product_description='" + info.getProductDescription() + "', " +
                "product_price='" + info.getProductPrice() + "', " +
                "putaway_number='" + info.getPutawayNumber() + "',surplus_number='" + info.getPutawayNumber() + "', " +
                "market_price='" + info.getMarketPrice() + "', `type`='" + info.getType() + "',number='" + info.getNumber() + "', remarks='" + info.getRemarks() + "'" +
                " WHERE id='" + info.getId() + "'";
        return sqlMapper.update(updateSql);
    }

    public String uploadPoint(MultipartFile picture, String cri) {
        //避免出现相同的图片名称  ids
        String ids = com.sxfq.loansupermarket.backsystem.core.util.CommUtils.getUUID();
        String httpUrl = "yingxiang/" + ids + (picture.getOriginalFilename()).substring(1, 5) + ".png";
        try {
            aliyunOssUtil.pushSingleFile(picture, httpUrl);
//            FileUtils.copyInputStreamToFile(picture.getInputStream(), new File(realPath, ids + (picture.getOriginalFilename()).substring(1,5)+".png"));
            String sql = "insert into point_reword_iconinfo (product_id, image_path,num,mainFigure) values " +
                    "('" + cri + "','" + MemberEnumConfig.ossphoeurl + httpUrl + "','" + cri + "','1')";
            sqlMapper.insert(sql); //上传主图
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return MemberEnumConfig.ossphoeurl + httpUrl;
    }

    public String upLoadProdcutTwo(MultipartFile picture, String cri) {
//        String realPath = "D:\\8068\\apache-tomcat-8.0.30\\webapps\\ROOT\\yingxiang";
        String productId = cri.split(",")[0];
        String mainFigure = cri.split(",")[1];
        //避免出现相同的图片名称  ids
        String ids = com.sxfq.loansupermarket.backsystem.core.util.CommUtils.getUUID();
//        String imageurl = httpUrl + "\\" + ids + (picture.getOriginalFilename()).substring(1,5)+".png";
        String httpUrl = "yingxiang/" + ids + (picture.getOriginalFilename()).substring(1, 5) + ".png";
        if ("0".equals(mainFigure)) {
            try {
                aliyunOssUtil.pushSingleFile(picture, httpUrl);
//                FileUtils.copyInputStreamToFile(picture.getInputStream(), new File(realPath, ids + (picture.getOriginalFilename()).substring(1,5)+".png"));
                String sql = "insert into point_reword_iconinfo (product_id, image_path) values " +
                        "('" + productId + "','" + MemberEnumConfig.ossphoeurl + httpUrl + "')";
                sqlMapper.insert(sql); //上传副图
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return MemberEnumConfig.ossphoeurl + httpUrl;
        } else {
            try {
//                FileUtils.copyInputStreamToFile(picture.getInputStream(), new File(realPath, ids + (picture.getOriginalFilename()).substring(1,5)+".png"));
                String sql = "insert into point_reword_iconinfo (product_id, image_path,mainFigure) values " +
                        "('" + productId + "','" + MemberEnumConfig.ossphoeurl + httpUrl + "','0')";
                sqlMapper.insert(sql); //上传详情图
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return MemberEnumConfig.ossphoeurl + httpUrl;
        }
    }

    public String updatePoint(MultipartFile picture, String cri) {
//        String realPath = "D:\\8068\\apache-tomcat-8.0.30\\webapps\\ROOT\\yingxiang";
        String num = cri.split(",")[0];
        //避免出现相同的图片名称  ids
        String ids = com.sxfq.loansupermarket.backsystem.core.util.CommUtils.getUUID();
//        String imageurl = httpUrl + "\\" + ids + (picture.getOriginalFilename()).substring(1,5)+".png";
        String httpUrl = "yingxiang/" + ids + (picture.getOriginalFilename()).substring(1, 5) + ".png";
        try {
            aliyunOssUtil.pushSingleFile(picture, httpUrl);
//            FileUtils.copyInputStreamToFile(picture.getInputStream(), new File(realPath, ids + (picture.getOriginalFilename()).substring(1,5)+".png"));
            String sql = "insert into point_reword_iconinfo (num, image_path) values " +
                    "('" + num + "','" + MemberEnumConfig.ossphoeurl + httpUrl + "')";
            sqlMapper.insert(sql);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return MemberEnumConfig.ossphoeurl + httpUrl;
    }

    public List<Map<String, Object>> list() {
        String sql = "select * from point_reword_info order by create_time asc;";
        return sqlMapper.selectList(sql);
    }
}